package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import model.Goods;
import model.GoodsSort;
import model.Manager;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IGoodsService;
import service.IGoodsSortService;
import service.IManagerService;
import service.IUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @aythor:侯仪澳
 * @data:2022/8/1
 */
//前台-用户类
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IGoodsSortService goodsSortService;

    //跳转的登录页面
    @RequestMapping("login")
    public String login(){
        return "shopping/login";
    }

    //进行用户登录的验证
    @RequestMapping("userLogin")
    @ResponseBody
    public String userLogin(User user, HttpServletRequest req){
        User user1 = userService.queryAllUser(user);
        if (user1 == null) {
            return "0";
        }
        //创建session
        HttpSession session = req.getSession();
        session.setAttribute("u", user1);
        session.setMaxInactiveInterval(6000);//设置有效期60s
        return user1.getId().toString();
    }

    //查用户名和手机号是否存在
    @RequestMapping("checkUser")
    @ResponseBody
    public String checkUser(User user){
        return userService.checkUser(user);
    }

    //
    @RequestMapping("loginUser")
    public ModelAndView mainPage(HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //根据id从session中获取管理员对象
        User user1 = (User) session.getAttribute("u");
        ModelAndView mv = new ModelAndView();
        mv.addObject("uName", user1.getuName());
        mv.addObject("uId", user1.getId());
        //查询父级分类
        List<GoodsSort> list = goodsSortService.queryOneSort(null);
        //创建json数组
        JSONArray arr = new JSONArray();
        for (GoodsSort s: list) {
            //集合元素转json对象
            JSONObject ele = (JSONObject) JSON.toJSON(s);

            //查询子分类
            List<GoodsSort> kidList = goodsSortService.queryOneSort(new GoodsSort(s.getId()));
            JSONArray kidArr = (JSONArray) JSON.toJSON(kidList);
            ele.put("kidArr", kidArr);
            arr.add(ele);
        }
        mv.addObject("arr", arr);
        mv.setViewName("shopping/mall_main");
        return mv;
    }

    //用户退出
    @RequestMapping("signOut")
    @ResponseBody
    public String signOut(HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //根据id从session中获取管理员对象
//        User user = (User) session.getAttribute("u");
        //注销session
        session.invalidate();
        return "1";
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("shopping/mall_main");
//        return modelAndView;

    }
    //商城分类菜单
    @RequestMapping("sortNav")
    public ModelAndView shopNav(){
        //查询父级分类
        List<GoodsSort> list = goodsSortService.queryOneSort(null);
        //创建json数组
        JSONArray arr = new JSONArray();
        for (GoodsSort s: list) {
            //集合元素转json对象
            JSONObject ele = (JSONObject) JSON.toJSON(s);

            //查询子分类
            List<GoodsSort> kidList = goodsSortService.queryOneSort(new GoodsSort(s.getId()));
            JSONArray kidArr = (JSONArray) JSON.toJSON(kidList);
            ele.put("kidArr", kidArr);
            arr.add(ele);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("arr", arr);
        modelAndView.setViewName("shopping/mall_main");
        return modelAndView;
    }

    //注册用户
    @RequestMapping("register")
    @ResponseBody
    public String register(User user){
        return userService.insertOneUser(user);
    }

    //进入用户页面
    @RequestMapping("userPage")
    public ModelAndView userPage(HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //根据id从session中获取管理员对象
        User user = (User) session.getAttribute("u");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", user);
        modelAndView.setViewName("shopping/mall_userInfoMain");
        return modelAndView;

    }

    //进入用户信息页面
    @RequestMapping("userInfo")
    public ModelAndView userInfo(HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //根据id从session中获取管理员对象
        User user = (User) session.getAttribute("u");
        User user1 = userService.queryOneUser(new User(user.getId()));
        session.setAttribute("u", user1);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", user1);
        modelAndView.setViewName("shopping/mall_userInfo");
        return modelAndView;
    }

    //用户余额页面
    @RequestMapping("userMoney")
    public ModelAndView userMoney(HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //根据id从session中获取管理员对象
        User user = (User) session.getAttribute("u");
        User user1 = userService.queryOneUser(new User(user.getId()));
        session.setAttribute("u", user1);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", user1);
        modelAndView.setViewName("shopping/mall_userMoney");
        return modelAndView;
    }

    //用户充值余额页面
    @RequestMapping("userMoneyIn")
    public ModelAndView userMoneyIn(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("shopping/mall_userMoneyIn");
        return modelAndView;
    }

    //修改用户信息
    @RequestMapping("updateUserInfo")
    @ResponseBody
    public String updateUserInfo(User user){
        return userService.updateUserInfo(user);
    }

    //用户余额充值
    @RequestMapping("rechargeMoney")
    @ResponseBody
    public String rechargeMoney(User user,HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //根据id从session中获取管理员对象
        User user1 = (User) session.getAttribute("u");
        user.setId(user1.getId());
        return userService.updateUserMoney(user);
    }
}
